package com.midian.controller.msg;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import com.midian.model.device.Device;
import com.midian.model.push.PushMsg;
import com.midian.model.user.User;
import com.midian.service.advice.IAdviceService;
import com.midian.service.device.IDeviceService;
import com.midian.service.push.IPushMsgService;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.midian.common.dao.OrderHelper;
import com.midian.common.filter.annotation.Validate;
import com.midian.common.pager.Pager;
import com.midian.common.system.ErrorConstants;
import com.midian.common.system.RetManager;
import com.midian.common.system.SysStatic;
import com.midian.common.utils.ContentJSONObject;
import com.midian.common.utils.ObjUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 消息推送控制器
 *
 */
@Controller("pushMsgController")
public class PushMsgController {
	/** 日志 */
	private static Logger LOG = Logger.getLogger(PushMsgController.class);
	/** 反馈意见业务 */
	private IPushMsgService pushMsgService;

	private IDeviceService deviceService;

	@Resource(name = "deviceService")
	public void setDeviceService(IDeviceService deviceService) {
		this.deviceService = deviceService;
	}

	@Resource(name = "pushMsgService")
	public void setPushMsgService(IPushMsgService pushMsgService) {
		this.pushMsgService = pushMsgService;
	}

	@Validate(retCode = RetManager.common_error)
	@RequestMapping(value = "/my_msgs", method = RequestMethod.GET)
	public @ResponseBody Object myMsgs(String client_key, String page, String size, HttpServletRequest request)
			throws Exception {
		JSONObject json = new JSONObject();
		try {
			Pager pager = new Pager();
			if (StringUtils.isNotBlank(page)) {
				pager.setCurrentPage(Integer.parseInt(page));
			}
			if (StringUtils.isNotBlank(size)) {
				pager.setPageSize(Integer.parseInt(size));
			}
			List<OrderHelper> orderHelpers = new ArrayList<OrderHelper>();
			OrderHelper orderHelper = new OrderHelper();
			orderHelper.setPropertyName("createTime");
			orderHelper.setType(OrderHelper.Type.DESC);
			orderHelpers.add(orderHelper);
			PushMsg target = new PushMsg();
			target.setDelFlag(SysStatic.NORMALDELFLAG);
			List<PushMsg> pushMsgs = pushMsgService.showList(new PushMsg(), pager, orderHelpers);
			JSONArray contentArray = new JSONArray();
			if (!pushMsgs.isEmpty()) {
				for (PushMsg pushMsg : pushMsgs) {
					ContentJSONObject jsonObject = new ContentJSONObject();
					jsonObject.put("title", pushMsg.getTitle());
					jsonObject.put("intro", pushMsg.getIntro());
					jsonObject.put("content", pushMsg.getContent());
					jsonObject.put("time", pushMsg.getCreateTime());
					contentArray.add(jsonObject.getContent());
					}
				}
				json.put("content", contentArray);
		} catch (Exception e) {
			LOG.error(e.getMessage());
			throw e;
		}
		return json;
	}
}
